/**
 * Created by zhourh on 2018/5/23.
 * 这题的思路主要考察对于位运算的理解，以及int型符号整数在二进制上的表示方式
 */
public class NumberComplement {

    public static void main(String[] args) {
        NumberComplement numberComplement = new NumberComplement();
        System.out.println(numberComplement.findComplement(5));
        System.out.println(numberComplement.findComplement(2));
        System.out.println(numberComplement.findComplement(1));
    }

    public int findComplement(int num) {
        int offset = 0;
        int result = num ^ 0xFFFFFFFF;
        while (result < 0) {
            result = result << 1;
            offset++;
        }

        result = result >> offset;

        return result;
    }

}
